﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Linq.SqlClient;
using System.Data.Objects.SqlClient;

namespace Hired
{
    /*  Company is linked to CompanyAccount.
     *  In other words, 1 Company belongs to 1 CompanyAccount.  */
    public class DBCompany
    {
        

        //add the company
        public static bool AddCompany(Company c)
        {
            try
            {
                using (hireddbEntities db = new hireddbEntities())
                {
                    db.SaveChanges();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        //retrieve the company
        public static Company RetrieveCompany(string AccountID) 
        {
            Company account = null;
            using (hireddbEntities db = new hireddbEntities()) 
            {
                    account =
                    (from a in db.Companies
                    where a.CAccountID == AccountID
                    select a).SingleOrDefault();
            }

            return account;
        }

        //update the company 
        public static bool UpdateCompany(Company c) 
        {
            try
            {
                using (hireddbEntities db = new hireddbEntities())
                {
                    Company accountToUpdate = RetrieveCompany(c.CAccountID);
                    db.Attach(accountToUpdate);
                    accountToUpdate.CompanyName = c.CompanyName;
                    accountToUpdate.DateFounded = c.DateFounded;
                    accountToUpdate.Address = c.Address;
                    accountToUpdate.PostalCode = c.PostalCode;
                    accountToUpdate.ImageAddress = c.ImageAddress;
                    accountToUpdate.WebsiteURL = c.WebsiteURL;
                    accountToUpdate.ContactNumber = c.ContactNumber;

                    db.SaveChanges();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        //delete the company
        public static bool DeleteCompany(Company c) 
        {
            try
            {
                Company companyToDelete = RetrieveCompany(c.CAccountID);
                using (hireddbEntities db = new hireddbEntities())
                {
                    db.Attach(companyToDelete);
                    db.DeleteObject(companyToDelete);
                    db.SaveChanges();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
        public static bool DeleteCompanyById(string id)
        {
            try
            {
                Company companyToDelete = RetrieveCompany(id);
                using (hireddbEntities db = new hireddbEntities())
                {
                    db.Attach(companyToDelete);
                    db.DeleteObject(companyToDelete);
                    db.SaveChanges();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
        //search for company
        public static List<Company> searchCompany(string searchQuery)
        {
            searchQuery = searchQuery.ToUpper();
            using (hireddbEntities db = new hireddbEntities())
            {
                var searchResults =
                    from a in db.Companies.Include("CompanyAccount")
                    where
                   a.CompanyName.ToUpper().Contains(searchQuery)
                    || a.Address.ToUpper().Contains(searchQuery)
                    || a.PostalCode.ToUpper().Contains(searchQuery)
                    || a.ContactNumber.Contains(searchQuery)
                    || a.WebsiteURL.ToUpper().Contains(searchQuery)
                    || a.CompanyAccount.Email.ToUpper().Contains(searchQuery)
                    select a;
                return searchResults.ToList<Company>();
            }
        }
        public static List<Company> advancedSearchCompany(List<Company> searchResults, string email, string exclude) {
            email = email.ToUpper();
            List<Company> advancedSearchResults = searchResults;
            //exclude
            for (int i = 0; i < advancedSearchResults.Count; i++)
            { 
                Company a = advancedSearchResults[i];
                if (a.CompanyName.ToUpper().Contains(exclude)
                    || a.Address.ToUpper().Contains(exclude)
                    || a.PostalCode.ToUpper().Contains(exclude)
                    || a.ContactNumber.Contains(exclude)
                    || a.WebsiteURL.ToUpper().Contains(exclude)
                    || a.CompanyAccount.Email.ToUpper().Contains(exclude)) {
                        advancedSearchResults.Remove(a);
                }
            }
            //check email
            for (int i = 0; i < advancedSearchResults.Count; i++)
            {
                if (email != "")
                {
                    if (!advancedSearchResults[i].CompanyAccount.Email.ToUpper().Contains(email))
                    {
                        advancedSearchResults.Remove(advancedSearchResults[i]);
                    }
                }
            }
            return advancedSearchResults;
        }
    }
}
